﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace CXW.Core
{
    /// <summary>
    /// Argument class used for <see cref="IDoozer.BuildItem"/>.
    /// </summary>
    public class BuildItemArgs
    {
        object caller;
        Codon codon;
        IEnumerable<ICondition> conditions;
        AddInTreeNode subItemNode;

        public BuildItemArgs(object caller, Codon codon, IEnumerable<ICondition> conditions, AddInTreeNode subItemNode)
        {
            if (codon == null)
                throw new ArgumentNullException("codon");
            this.caller = caller;
            this.codon = codon;
            this.conditions = conditions ?? Enumerable.Empty<ICondition>();
            this.subItemNode = subItemNode;
        }

        /// <summary>
        /// The caller passed to <see cref="AddInTree.BuildItem(string,object)"/>.
        /// </summary>
        public object Caller
        {
            get { return caller; }
        }

        /// <summary>
        /// The codon to build.
        /// </summary>
        public Codon Codon
        {
            get { return codon; }
        }

        /// <summary>
        /// The conditions applied to this item.
        /// </summary>
        public IEnumerable<ICondition> Conditions
        {
            get { return conditions; }
        }

        /// <summary>
        /// Builds the sub-items.
        /// Conditions on this node are also applied to the sub-nodes.
        /// </summary>
        public List<T> BuildSubItems<T>()
        {
            if (subItemNode == null)
                return new List<T>();
            else
                return subItemNode.BuildChildItems<T>(caller, conditions);
        }
    }
}
